package com.mooc.ppjoke.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.mooc.lib_common.LiveDataBus;
import com.mooc.ppjoke.BR;
import com.mooc.ppjoke.data.Feed;
import com.mooc.ppjoke.databinding.LayoutFeedTypeImageBinding;
import com.mooc.ppjoke.databinding.LayoutFeedTypeVideoBinding;
import com.mooc.ppjoke.extention.AbsPagedListAdapter;
import com.mooc.ppjoke.ui.detail.FeedDetailActivity;
import com.mooc.ppjoke.ui.my_view.NetPlayerView;

import org.jetbrains.annotations.NotNull;

/**
 * @ClassName: FeedAdapter$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/18 13:49
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class FeedAdapter extends AbsPagedListAdapter<Feed, FeedAdapter.ViewHolder> {
    private final LayoutInflater inflater;
    protected Context context;
    protected String category;
    protected FeedAdapter(Context context, String category) {
        super(new DiffUtil.ItemCallback<Feed>() {
            @Override
            public boolean areItemsTheSame(@NonNull @NotNull Feed oldItem, @NonNull @NotNull Feed newItem) {
                return oldItem.id==newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull @NotNull Feed oldItem, @NonNull @NotNull Feed newItem) {
                return oldItem.equals(newItem);
            }
        });
        this.inflater = LayoutInflater.from(context);
        this.context = context;
        this.category = category;
    }

    @Override
    public int getItemViewType(int position) {
        Feed item = getItem(position);
        return item.itemType;
    }
    private FeedObserver mFeedObserver;

    private class FeedObserver implements Observer<Feed> {

        private Feed mFeed;

        @Override
        public void onChanged(Feed newOne) {
            if (mFeed.id != newOne.id)
                return;
            mFeed.author = newOne.author;
            mFeed.ugc = newOne.ugc;
            mFeed.notifyChange();
        }

        public void setFeed(Feed feed) {

            mFeed = feed;
        }
    }

//    @NonNull
//    @NotNull
//    @Override
//    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
//        ViewDataBinding binding;
//        if(viewType==Feed.TYPE_IMAGE){
//             binding=LayoutFeedTypeImageBinding.inflate(inflater);
//        }else {
//             binding = LayoutFeedTypeVideoBinding.inflate(inflater);
//        }
//        return new ViewHolder(binding.getRoot(),binding);
//    }

    @Override
    protected ViewHolder onCreateViewHolder2(ViewGroup parent, int viewType) {
        ViewDataBinding binding;
        if(viewType==Feed.TYPE_IMAGE){
             binding=LayoutFeedTypeImageBinding.inflate(inflater);
        }else {
             binding = LayoutFeedTypeVideoBinding.inflate(inflater);
        }
        return new ViewHolder(binding.getRoot(),binding);
    }

//    @Override
//    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
//        holder.bindData(getItem(position));
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                FeedDetailActivity.startFeedDetailActivity(context, getItem(position), category);
//                onStartFeedDetailActivity(getItem(position));
//                if (mFeedObserver == null) {
//                    mFeedObserver = new FeedObserver();
//                    LiveDataBus.get()
//                            .with(InteractionPresenter.DATA_FROM_INTERACTION)
//                            .observe((LifecycleOwner) context, mFeedObserver);
//                }
//                mFeedObserver.setFeed(getItem(position));
//            }
//        });
//    }

    @Override
    protected void onBindViewHolder2(ViewHolder holder, int position) {
        holder.bindData(getItem(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FeedDetailActivity.startFeedDetailActivity(context, getItem(position), category);
                onStartFeedDetailActivity(getItem(position));
                if (mFeedObserver == null) {
                    mFeedObserver = new FeedObserver();
                    LiveDataBus.get()
                            .with(InteractionPresenter.DATA_FROM_INTERACTION)
                            .observe((LifecycleOwner) context, mFeedObserver);
                }
                mFeedObserver.setFeed(getItem(position));
            }
        });
    }

    public void onStartFeedDetailActivity(Feed feed) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ViewDataBinding binding;
        private NetPlayerView netPlayView;

        public ViewHolder(@NonNull @NotNull View itemView, ViewDataBinding binding) {
            super(itemView);
            this.binding = binding;
        }

        public void bindData(Feed feed){
            binding.setVariable(BR.lifeCycleOwner,context);
            if(binding instanceof LayoutFeedTypeImageBinding){
                LayoutFeedTypeImageBinding imageBinding = (LayoutFeedTypeImageBinding) this.binding;
                imageBinding.setFeed(feed);
                imageBinding.feedImage.bindData(feed.width, feed.height, 16,feed.cover);
            }else {
                LayoutFeedTypeVideoBinding videoBinding = (LayoutFeedTypeVideoBinding) this.binding;
                videoBinding.setFeed(feed);
                netPlayView = videoBinding.netPlayView;
                netPlayView.bindData(category,feed.width,feed.height,feed.cover, feed.url);
            }
        }
        public boolean isVideoItem(){
            return binding instanceof LayoutFeedTypeVideoBinding;
        }

        public NetPlayerView getNetPlayerView(){
            return netPlayView;
        }
    }
}
